<!-- src/views/Admin.vue -->
<template>
  <div class="admin">
    <div class="admin-header">
      <h1>
        <img src="@/assets/icons/svg/monitor.svg" alt="icon" style="width: 24px; height: 24px; margin-right: 8px; vertical-align: middle" />
        管理后台
      </h1>
      <p>商品和用户管理</p>
    </div>

    <div class="admin-nav">
      <button class="nav-btn" :class="{ active: activeTab === 'products' }" @click="setActiveTab('products')">商品管理</button>
      <button class="nav-btn" :class="{ active: activeTab === 'users' }" @click="setActiveTab('users')">👥 用户管理</button>
    </div>

    <!-- 商品管理页面 -->
    <div v-if="activeTab === 'products'" class="admin-content">
      <div class="section-header">
        <h2>商品管理</h2>
        <button class="add-btn" @click="showAddProduct = true">➕ 新增商品</button>
      </div>

      <!-- 商品列表 -->
      <div class="data-table">
        <table>
          <thead>
            <tr>
              <th>图片</th>
              <th>商品名称</th>
              <th>价格</th>
              <th>类型</th>
              <th>度数</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="product in products" :key="product.id">
              <td>
                <img :src="product.image" :alt="product.name" class="product-img" />
              </td>
              <td>{{ product.name }}</td>
              <td>¥{{ product.price }}</td>
              <td>{{ product.type }}</td>
              <td>{{ product.stock }}</td>
              <td>
                <span :class="['status', product.status === '上架' ? 'active' : 'inactive']">
                  {{ product.status }}
                </span>
              </td>
              <td>
                <button class="edit-btn" @click="editProduct(product)">编辑</button>
                <button class="delete-btn" @click="deleteProduct(product.id)">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>

      <!-- 新增/编辑商品弹窗 -->
      <div v-if="showAddProduct || editingProduct" class="modal-overlay" @click="closeProductModal">
        <div class="modal" @click.stop>
          <div class="modal-header">
            <h3>{{ editingProduct ? '编辑商品' : '新增商品' }}</h3>
            <button class="close-btn" @click="closeProductModal">✕</button>
          </div>
          <form @submit.prevent="saveProduct" class="product-form">
            <div class="form-group">
              <label>商品名称 *</label>
              <input v-model="productForm.name" type="text" required placeholder="请输入商品名称" />
            </div>
            <div class="form-row">
              <div class="form-group">
                <label>价格 *</label>
                <input v-model.number="productForm.price" type="number" step="0.01" required placeholder="0.00" />
              </div>
              <div class="form-group">
                <label>度数 *</label>
                <input v-model.number="productForm.stock" type="number" required placeholder="0" />
              </div>
            </div>
            <div class="form-row">
              <div class="form-group">
                <label>类型 *</label>
                <select v-model="productForm.type" required>
                  <option value="">请选择类型</option>
                  <option value="纯白酒">纯白酒</option>
                  <option value="玉米酒">玉米酒</option>
                  <option value="高粱酒">高粱酒</option>
                  <option value="金樱子酒">金樱子酒</option>
                </select>
              </div>
              <div class="form-group">
                <label>状态 *</label>
                <select v-model="productForm.status" required>
                  <option value="上架">上架</option>
                  <option value="下架">下架</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label>商品图片</label>
              <input v-model="productForm.image" type="url" placeholder="请输入图片URL" />
            </div>
            <div class="form-group">
              <label>商品描述</label>
              <textarea v-model="productForm.description" rows="3" placeholder="请输入商品描述"></textarea>
            </div>
            <div class="form-actions">
              <button type="button" class="cancel-btn" @click="closeProductModal">取消</button>
              <button type="submit" class="save-btn">
                {{ editingProduct ? '更新' : '保存' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 用户管理页面 -->
    <div v-if="activeTab === 'users'" class="admin-content">
      <div class="section-header">
        <h2>用户管理</h2>
        <button class="add-btn" @click="showAddUser = true">➕ 新增用户</button>
      </div>

      <!-- 用户列表 -->
      <div class="data-table">
        <table>
          <thead>
            <tr>
              <th>头像</th>
              <th>用户名</th>
              <th>邮箱</th>
              <th>电话</th>
              <th>角色</th>
              <th>状态</th>
              <th>注册时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="user in users" :key="user.id">
              <td>
                <div class="user-avatar">
                  {{ user.username.charAt(0).toUpperCase() }}
                </div>
              </td>
              <td>{{ user.username }}</td>
              <td>{{ user.email }}</td>
              <td>{{ user.phone || '未填写' }}</td>
              <td>
                <span :class="['role', user.role]">
                  {{ user.role === 'admin' ? '管理员' : '用户' }}
                </span>
              </td>
              <td>
                <span :class="['status', user.status === '正常' ? 'active' : 'inactive']">
                  {{ user.status }}
                </span>
              </td>
              <td>{{ formatDate(user.createdAt) }}</td>
              <td>
                <button class="edit-btn" @click="editUser(user)">编辑</button>
                <button class="delete-btn" @click="deleteUser(user.id)">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>

      <!-- 新增/编辑用户弹窗 -->
      <div v-if="showAddUser || editingUser" class="modal-overlay" @click="closeUserModal">
        <div class="modal" @click.stop>
          <div class="modal-header">
            <h3>{{ editingUser ? '编辑用户' : '新增用户' }}</h3>
            <button class="close-btn" @click="closeUserModal">✕</button>
          </div>
          <form @submit.prevent="saveUser" class="user-form">
            <div class="form-group">
              <label>用户名 *</label>
              <input v-model="userForm.username" type="text" required placeholder="请输入用户名" />
            </div>
            <div class="form-group">
              <label>邮箱 *</label>
              <input v-model="userForm.email" type="email" required placeholder="请输入邮箱" />
            </div>
            <div class="form-row">
              <div class="form-group">
                <label>电话</label>
                <input v-model="userForm.phone" type="tel" placeholder="请输入电话号码" />
              </div>
              <div class="form-group">
                <label>角色 *</label>
                <select v-model="userForm.role" required>
                  <option value="user">用户</option>
                  <option value="admin">管理员</option>
                </select>
              </div>
            </div>
            <div class="form-row">
              <div class="form-group">
                <label>状态 *</label>
                <select v-model="userForm.status" required>
                  <option value="正常">正常</option>
                  <option value="禁用">禁用</option>
                </select>
              </div>
              <div class="form-group" v-if="!editingUser">
                <label>密码 *</label>
                <input v-model="userForm.password" type="password" :required="!editingUser" placeholder="请输入密码" />
              </div>
            </div>
            <div class="form-actions">
              <button type="button" class="cancel-btn" @click="closeUserModal">取消</button>
              <button type="submit" class="save-btn">
                {{ editingUser ? '更新' : '保存' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'

// 响应式数据
const activeTab = ref('products')
const showAddProduct = ref(false)
const showAddUser = ref(false)
const editingProduct = ref(null)
const editingUser = ref(null)

// 商品数据
const products = ref([
  {
    id: 1,
    name: '法国波尔多红酒',
    price: 299,
    type: '红酒',
    stock: 50,
    status: '上架',
    image: 'https://cdn.pixabay.com/photo/2017/02/01/11/18/alcohol-2029735_1280.png',
    description: '来自法国波尔多地区的优质红酒'
  },
  {
    id: 2,
    name: '意大利托斯卡纳白酒',
    price: 199,
    type: '白酒',
    stock: 30,
    status: '上架',
    image: 'https://cdn.pixabay.com/photo/2019/08/10/22/30/irish-4397841_1280.png',
    description: '清香淡雅的意大利白酒'
  }
])

// 用户数据
const users = ref([
  {
    id: 1,
    username: 'admin',
    email: 'admin@wineshop.com',
    phone: '13800138000',
    role: 'admin',
    status: '正常',
    createdAt: new Date('2024-01-15')
  },
  {
    id: 2,
    username: 'john_doe',
    email: 'john@example.com',
    phone: '13900139000',
    role: 'user',
    status: '正常',
    createdAt: new Date('2024-02-20')
  }
])

// 表单数据
const productForm = reactive({
  name: '',
  price: '',
  type: '',
  stock: '',
  status: '上架',
  image: '',
  description: ''
})

const userForm = reactive({
  username: '',
  email: '',
  phone: '',
  role: 'user',
  status: '正常',
  password: ''
})

// 方法
const setActiveTab = tab => {
  activeTab.value = tab
}

const resetProductForm = () => {
  Object.assign(productForm, {
    name: '',
    price: '',
    type: '',
    stock: '',
    status: '上架',
    image: '',
    description: ''
  })
}

const resetUserForm = () => {
  Object.assign(userForm, {
    username: '',
    email: '',
    phone: '',
    role: 'user',
    status: '正常',
    password: ''
  })
}

const closeProductModal = () => {
  showAddProduct.value = false
  editingProduct.value = null
  resetProductForm()
}

const closeUserModal = () => {
  showAddUser.value = false
  editingUser.value = null
  resetUserForm()
}

const saveProduct = () => {
  if (editingProduct.value) {
    // 编辑商品
    const index = products.value.findIndex(p => p.id === editingProduct.value.id)
    if (index !== -1) {
      products.value[index] = {
        ...editingProduct.value,
        ...productForm
      }
    }
  } else {
    // 新增商品
    const newProduct = {
      id: Date.now(),
      ...productForm
    }
    products.value.push(newProduct)
  }
  closeProductModal()
}

const saveUser = () => {
  if (editingUser.value) {
    // 编辑用户
    const index = users.value.findIndex(u => u.id === editingUser.value.id)
    if (index !== -1) {
      users.value[index] = {
        ...editingUser.value,
        ...userForm
      }
    }
  } else {
    // 新增用户
    const newUser = {
      id: Date.now(),
      ...userForm,
      createdAt: new Date()
    }
    users.value.push(newUser)
  }
  closeUserModal()
}

const editProduct = product => {
  editingProduct.value = product
  Object.assign(productForm, product)
  showAddProduct.value = true
}

const editUser = user => {
  editingUser.value = user
  Object.assign(userForm, {
    ...user,
    password: '' // 编辑时不显示密码
  })
  showAddUser.value = true
}

const deleteProduct = id => {
  if (confirm('确定要删除这个商品吗？')) {
    const index = products.value.findIndex(p => p.id === id)
    if (index !== -1) {
      products.value.splice(index, 1)
    }
  }
}

const deleteUser = id => {
  if (confirm('确定要删除这个用户吗？')) {
    const index = users.value.findIndex(u => u.id === id)
    if (index !== -1) {
      users.value.splice(index, 1)
    }
  }
}

const formatDate = date => {
  return new Date(date).toLocaleDateString('zh-CN')
}

onMounted(() => {
  console.log('WineShop 管理后台已加载')
})
</script>

<style scoped>
.admin {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.admin-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px;
}

.admin-header h1 {
  margin: 0 0 10px 0;
  font-size: 2rem;
}

.admin-header p {
  margin: 0;
  opacity: 0.9;
}

.admin-nav {
  display: flex;
  gap: 10px;
  margin-bottom: 30px;
  justify-content: center;
}

.nav-btn {
  padding: 12px 24px;
  border: none;
  background: #f8f9fa;
  color: #495057;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s ease;
}

.nav-btn:hover {
  background: #e9ecef;
}

.nav-btn.active {
  background: #007bff;
  color: white;
}

.admin-content {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.section-header h2 {
  margin: 0;
  color: #333;
}

.add-btn {
  padding: 10px 20px;
  background: #28a745;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.3s ease;
}

.add-btn:hover {
  background: #218838;
}

.data-table {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
}

th,
td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  color: #495057;
}

.product-img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 6px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #007bff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status.active {
  background: #d4edda;
  color: #155724;
}

.status.inactive {
  background: #f8d7da;
  color: #721c24;
}

.role {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.role.admin {
  background: #fff3cd;
  color: #856404;
}

.role.user {
  background: #d1ecf1;
  color: #0c5460;
}

.edit-btn,
.delete-btn {
  padding: 5px 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  margin-right: 5px;
  transition: all 0.3s ease;
}

.edit-btn {
  background: #17a2b8;
  color: white;
}

.edit-btn:hover {
  background: #138496;
}

.delete-btn {
  background: #dc3545;
  color: white;
}

.delete-btn:hover {
  background: #c82333;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.modal-header h3 {
  margin: 0;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #333;
}

.product-form,
.user-form {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #333;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s ease;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #007bff;
}

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.cancel-btn,
.save-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: #6c757d;
  color: white;
}

.cancel-btn:hover {
  background: #5a6268;
}

.save-btn {
  background: #007bff;
  color: white;
}

.save-btn:hover {
  background: #0056b3;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin {
    padding: 10px;
  }

  .admin-nav {
    flex-direction: column;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .data-table {
    font-size: 14px;
  }

  th,
  td {
    padding: 8px;
  }
}
</style>
